
#include "..\headers\ListOf.h"
#define Scanf 1
#define unScanf 0

#define InitSize \
  100  // when danamically allocating storage ，unlimited maximum,given only one
       // initial value
#define ListIncrement 10  // allocate increment

typedef struct {
  int data;
  int index;
} *DataGroup;

typedef struct {
  DataGroup Data;
  int length;
  int listsize;
} SeqList;

int InitSeqList(SeqList &L) {
  L.Data = (DataGroup)malloc(sizeof(SeqList) * InitSize);
  if (!L.Data) return ERROR;
  L.length = 0;
  L.listsize = InitSize;
  return OK;
}

int CreatSeqList(SeqList &L, int num, int flag) {
  // Num represents the creation length, while flag
  // represents whether data needs to be input. 1
  if (num <= 0) {
    return ERROR;
  }
  int nums = num;
  if (num > L.listsize) {
    DataGroup data;
    data = (DataGroup)realloc(
        L.Data,
        sizeof(SeqList) *
            (ListIncrement + L.listsize));  // If there is insufficient memory,
                                            // add ListIncrement units
    if (!data) return ERROR;
    L.Data = data;
    L.listsize += ListIncrement;
  }
  if (flag) {
    for (int i = 0; i < num; i++) {
      int data, index;
      printf("please input parameter and index:");
      scanf("%d %d", &data, &index);
      if (i != 0 && index == L.Data[i - 1].index) {
        L.Data[i - 1].data += data;
        nums--;
      } else {
        L.Data[i].data = data;
        L.Data[i].index = index;
      }
    }
  }
  L.length = nums;
  return OK;
}

int AddSeqLists(SeqList &L, SeqList &Q, SeqList &Res) {
  int i = 0, j = 0;
  int s = 0;
  if (L.length == 0 && Q.length != 0) {
    Res = Q;
    return OK;
  }
  if (L.length != 0 && Q.length == 0) {
    Res = L;
    return OK;
  }
  while (i < L.length && j < Q.length) {
    if (L.Data[i].index >
        Q.Data[j].index) {  // If the index in L is greater than the index in Q,
                            // record the Q data. The same applies below
      Res.Data[s] = Q.Data[j];
      j++, s++;
      continue;
    }
    if (L.Data[i].index < Q.Data[j].index) {
      Res.Data[s] = L.Data[i];
      i++, s++;
      continue;
    }
    if (L.Data[i].index == Q.Data[j].index) {
      Res.Data[s].data = L.Data[i].data + Q.Data[j].data;
      Res.Data[s].index = L.Data[i].index;
      s++, i++, j++;
      continue;
    }
  }
  if (i < L.length) {  // Splicing the remaining elements
    for (i; i < L.length; i++) {
      Res.Data[s++] = L.Data[i];
    }
  }
  if (j < Q.length) {
    for (j; j < Q.length; j++) {
      Res.Data[s++] = Q.Data[j];
    }
  }
  Res.length = s;
  return OK;
}

int SubtractionLists(SeqList &L, SeqList &Q, SeqList &Res) {
  int i = 0, j = 0;
  int s = 0;
  if (L.length == 0 && Q.length != 0) {
    for (i = 0; i < Q.length; i++) {
      Q.Data[i].data = -Q.Data[i].data;
    }
    Res = Q;
    return OK;
  }
  if (L.length != 0 && Q.length == 0) {
    Res = L;
    return OK;
  }
  while (i < L.length && j < Q.length) {
    if (L.Data[i].index >
        Q.Data[j].index) {  // If the index in L is greater than the index in Q,
                            // record the Q data. The same applies below
      Res.Data[s] = Q.Data[j];
      j++, s++;
      continue;
    }
    if (L.Data[i].index < Q.Data[j].index) {
      Res.Data[s] = L.Data[i];
      i++, s++;
      continue;
    }
    if (L.Data[i].index == Q.Data[j].index) {
      Res.Data[s].data = L.Data[i].data - Q.Data[j].data;
      Res.Data[s].index = L.Data[i].index;
      s++, i++, j++;
      continue;
    }
  }
  if (i < L.length) {  // Splicing the remaining elements
    for (i; i < L.length; i++) {
      Res.Data[s++] = L.Data[i];
    }
  }
  if (j < Q.length) {
    for (j; j < Q.length; j++) {
      Res.Data[s++] = Q.Data[j];
    }
  }
  Res.length = s;
  return OK;
}

int DistoryList(SeqList &L) {
  if (!L.Data) {
    printf("Did not exist");
    return ERROR;
  }
  free(L.Data);
  L.length = 0;
  L.listsize = 0;
  L.Data = NULL;  // Prevent the occurrence of wild pointers
  printf("Distory Successfully");
  return OK;
}

int main() {
  int num1, num2;
  SeqList L, Q, Res;
  if (InitSeqList(L) && InitSeqList(Q) && InitSeqList(Res)) {
    printf("please in put length");
    scanf("%d", &num1);
    CreatSeqList(L, num1, Scanf);
    printf("please in put length");
    scanf("%d", &num2);
    CreatSeqList(Q, num2, Scanf);
    int flag;
    while (1) {
      printf("please input a number 1 represent add 0 represent subtraction:");
      scanf("%d", &flag);
      if (flag == 1 || flag == 0)
        break;
      else
        printf("ERROR SCANF");
    }
    CreatSeqList(Res, num1 + num2, unScanf);
    if (L.length == 0 && Q.length == 0) return ERROR;
    if (flag == 1) AddSeqLists(L, Q, Res);
    if (flag == 0) SubtractionLists(L, Q, Res);

    flag = 0;
    for (int i = 0; i < Res.length; i++) {
      if (Res.Data[i].data != 0) {
        if (Res.Data[i].data != 1) printf("%d", Res.Data[i].data);
        if (Res.Data[i].index != 0 && Res.Data[i].index != 1)
          printf("x^%d", Res.Data[i].index);
        if (Res.Data[i].index == 1) printf("x");
        if ((Res.Data[i + 1].data > 0) && (i < Res.length - 1)) printf("+");
        flag = 1;
      }
    }
    if (flag == 0) {
      printf("0");
    }
  }
  return OK;
}
